package com.rich.common.util;

import com.rich.common.entity.TbHouse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * @author minicoco
 * @version 1.0
 * @date 2021/8/23 18:39
 */
public class HtmlParseUtil {


    //获取连接
    public static Document getDocument(String url){
        try {

            return Jsoup.connect(url).get();

        } catch (IOException e) {
            e.printStackTrace();

        }
        return null;
    }

    public static ArrayList<TbHouse> parseZuFangHtml(Document document) {
        //解析html 标签
        // 通过类名来获取
        Elements elements = document.getElementsByClass("pList");
//        System.out.println(elements);
        //创建集合
        ArrayList<TbHouse> arrayList = new ArrayList(1024);
        int i = 0;
        //遍历页面中的房源信息
        for (Element element : elements.select("li")) {

            //创建实体类
            TbHouse demo = new TbHouse();

            //获取图片
            Elements img = element.select("div.listImg").select("img");
            //获取图片路径
            String src = img.attr("src");
            if (src==null || src.length()<1){
                src = img.attr("data-src");
            }
            //去除没有图片的li
            if (src==null||src.length()<1){
                continue;
            }
            //图片路径赋值
            demo.setImage(src);
            //解析房源名称
            String html = element.select("h3.listTit").select("a").html();
            //房源名称赋值
            demo.setTitle(html);
            //解析房源信息
            Elements p = element.select("div.listX").select("p");
            if (p!=null || p.hasText()){
                String[] split = p.text().trim().split(" · ");

//                for (String s : split) {
//                    System.out.println(s);
//                }
                //面积
                demo.setArea(BigDecimal.valueOf(Double.parseDouble(split[1].substring(0,split[1].indexOf(" ")))));
                //单价
                String price = split[split.length - 1].substring(split[split.length - 1].indexOf("价")+1, split[split.length - 1].indexOf("元"));
                demo.setPrice(BigDecimal.valueOf(Double.parseDouble(price)));
                //装修
                if ("简装".equals(split[4])){
                    demo.setFinish("1");
                }else if ("精装".equals(split[4])){
                    demo.setFinish("2");
                }else {
                    demo.setFinish("0");
                }
                //朝向
                if ("东".equals(split[2])){
                    demo.setOrientation("0");
                }else if ("西".equals(split[2])){
                    demo.setOrientation("1");
                }else if ("北".equals(split[2])){
                    demo.setOrientation("2");
                }else if ("南".equals(split[2])){
                    demo.setOrientation("2");
                }else if ("南北".equals(split[2])){
                    demo.setOrientation("4");
                }
                //所在楼层数
                String floor = split[3].substring(split[3].indexOf("/")+1,split[3].lastIndexOf("层"));
                demo.setFloorNum(Integer.valueOf(floor));
                //室 卫 户
                String room = split[0].substring(0, 1);
                demo.setRoom(Integer.valueOf(room));
                String toilet = split[0].substring(split[0].indexOf("室")+1, split[0].indexOf("厅"));
                demo.setToilet(Integer.valueOf(toilet));
                String hall = split[0].substring(split[0].indexOf("厅")+1, split[0].indexOf("卫"));
                demo.setHall(Integer.valueOf(hall));
                //合兴买点
                demo.setSellingPoint(split[6]);
            }
            //解析总价格
            String strong = element.select("div.jia").select("p.redC").select("strong").text();
            demo.setSumPrice(BigDecimal.valueOf(Double.parseDouble(strong)));

            //将实体类加入集合
            arrayList.add(demo);
            i++;
        }

        System.out.println("共有" + i + "条记录!");
        ArrayList<TbHouse> houseArrayList = new ArrayList<TbHouse>(64);
        for (TbHouse tbHouse : arrayList) {
            tbHouse.setCheckTrue("0");
            tbHouse.setHouseType(2);
            tbHouse.setSort(0);
            tbHouse.setHouseForm(1);
            tbHouse.setIsDelete("0");
            tbHouse.setStatus("0");
            tbHouse.setIsClinch("0");
            tbHouse.setBrokerId(1L);
            houseArrayList.add(tbHouse);
            System.out.println(tbHouse);
        }
        return houseArrayList;
    }
}
